<?php
require_once 'libs/configTest.php';
require_once 'class/admin.php';
require_once 'class/roster.php';

$testConfigTest = new DBconfigTest();
$response;
$dutyArray = array();

session_start();
$admin = unserialize($_SESSION['admin']);

if (isset($_REQUEST["newDBName"])) {
    $newDBName = $_REQUEST["newDBName"];
}
if (isset($_REQUEST["newDutyArray"])) {
    $dutyArray = unserialize($_REQUEST["newDutyArray"]);
}
if (isset($_REQUEST["tip"])) {
    $tip = $_REQUEST["tip"];
}
if (isset($_REQUEST["dutyNum"])) {
    $dutyNum = $_REQUEST["dutyNum"] + 1;
    for ($i = 0; $i < $dutyNum; $i++) {
        $dutyArray[$i] = $_REQUEST["duty" . $i];
    }
}
if (isset($_REQUEST["startDate"])) {
    $startDate = $_REQUEST["startDate"];
}
if (isset($_REQUEST["endDate"])) {
    $endDate = $_REQUEST["endDate"];
}


if ($tip == 1) {
    $response = $testConfigTest->DBCheck($newDBName);
} else if ($tip == 2) {
    $response = $testConfigTest->initalDB($newDBName);
    if ($response == "create DB successful") {
        $testConfigTest->updateRosterDBToUserDB($admin->getID(), $newDBName);
        $admin->setDBName($newDBName);
        $initialNum = $admin->getInitial();
        $testConfigTest->updateInitialState($admin->getID(), $initialNum + 1);
        $admin->setInitial($initialNum + 1);
        $_SESSION['admin'] = serialize($admin);
    }
} else if ($tip == 3) {
    $testConfigTest->InitialRosterDutiesArray($admin->getDBName(), $dutyArray);
    $initialNum = $admin->getInitial();
    $testConfigTest->updateInitialState($admin->getID(), $initialNum + 1);
    $admin->setInitial($initialNum + 1);
    $admin->setDutyArray($dutyArray);
    $_SESSION['admin'] = serialize($admin);
    $response = "initial duty list successful";
    /////need to read all the roster class inforamtion into session
} else if ($tip == 4) {
    $roster = new roster();
    $roster->setStarEndDate($startDate, $endDate);
    $roster->setDuitesArray($admin->getDutyArray());
    $testConfigTest->initialRosterTable($roster->getDutyArray(),$roster->getSundaysArray(),$admin->getDBName(),1);
    $admin->setRoster($roster);
   
    $initialNum = $admin->getInitial();
    $testConfigTest->updateInitialState($admin->getID(), $initialNum + 1);
    $admin->setInitial($initialNum + 1);
    $_SESSION['admin'] = serialize($admin);
    $response = "initial roster successful";
    
}

//output the response
echo $response;
?>